<?php
declare (strict_types=1);

namespace app\controller;

use app\BaseController;
use app\common\Express;
use app\common\Pay;
use app\job\Task;
use app\model\Address;
use app\model\Config;
use app\model\Goods;
use app\model\GoodsClass;
use app\model\GoodsCoupon;
use app\model\GoodsOrders;
use app\model\GoodsOrdersAssess;
use app\model\GoodsShopcar;
use app\model\GoodsShopcarOrders;
use app\model\GoodsSpec;
use app\model\User;
use app\model\UserCollect;
use app\model\UserCoupon;
use app\service\UserService;
use app\tool\Random;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\db\Query;
use think\Exception;
use think\facade\Db;
use think\facade\Queue;
use think\Request;

class GoodsController extends BaseController
{
	protected $order_timeout = 60 * 10; //订单超时时间
	protected $middleware = [
		'Login' => ['except' => ['class']],
	];
	
	/**
	 * 商品分类
	 * @return void
	 */
	function class()
	{
		$rows = GoodsClass::order(['weigh' => 'desc'])->select();
		$this->writeJson($rows);
	}
	
	/**
	 * 商品列表
	 * @return void
	 */
	function index(Request $request)
	{
		$class_id = $request->get('class_id/d', 0);
		$keyword = $request->get('keyword', '');
		$price = $request->get('price', '');
		$sales = $request->get('sales', '');
		$user = User::findOrFail($this->user_id);
		$goods_ids = $user->school->goods_ids;
		$row = Goods::where([['id', 'in', $goods_ids]])
			->withMin(['spec' => 'price'], 'price')
			->when(!empty($class_id), function (Query $query) use ($class_id) {
				$query->where(['class_id' => $class_id]);
			})
			->when(!empty($keyword), function (Query $query) use ($keyword) {
				$query->where('name', 'like', '%' . trim($keyword) . '%');
			})
			->when(!empty($price), function (Query $query) use ($price) {
				$query->order('price', $price);
			})
			->when(!empty($sales), function (Query $query) use ($sales) {
				$query->order('sales', $sales);
			})
			->filter(function ($data) {
				$data->images = explode(',', $data->images);
				$data->videos = explode(',', $data->videos);
				
			})
			->paginate()
			->items();
		
		$this->writeJson($row);
	}
	
	/**
	 * 商品详情
	 * @param Request $request
	 */
	public function detail(Request $request)
	{
		$id = $request->get('id/d');
		$row = Goods::with([
			'assess' => function (Query $query) {
				$query->with(['user', 'orders' => function (Query $query) {
					$query->with(['spec']);
				}])->limit(5);
			},
			'spec'=>function (Query $query) {
				$query->order(['weigh'=>'desc']);
			}
		])
			->withCount('assess')
			->withMin(['spec' => 'min_price'], 'price')
			->withCount(['assess' => function (Query $query, &$alias) {
				$query->where(['score' => 5]);
				$alias = 'assess_good_count';
			}])
			->withCount(['assess' => function (Query $query, &$alias) {
				$query->whereIn('score', [3, 4]);
				$alias = 'assess_normal_count';
			}])
			->withCount(['assess' => function (Query $query, &$alias) {
				$query->whereIn('score', [1, 2]);
				$alias = 'assess_bad_count';
			}])
			->withCount(['assess' => function (Query $query, &$alias) {
				$query->where('images', '<>', '');
				$alias = 'assess_images_count';
			}])
			->filter(function ($data) {
				$data->images = explode(',', $data->images);
				$data->videos = explode(',', $data->videos);
				$coupon = GoodsCoupon::where(function (Query $query) use ($data) {
					$query->whereOr('goods_id', $data->id)->whereOr('scene', 2);
				})->where('num', '>', 0)->filter(function ($data) {
					$UserCoupon = UserCoupon::where(['coupon_id' => $data->id, 'user_id' => $this->user_id])->find();
					if ($UserCoupon) {
						$data->receive = 1;
					} else {
						$data->receive = 0;
					}
				})->select();
				$data->coupon = $coupon;
				UserCollect::where(['user_id'=>$this->user_id,'goods_id'=>$data->id])->find() ? $data->user_collect = true : $data->user_collect = false;
				$data->shopcar_count = GoodsShopcar::where(['user_id' => $this->user_id])->count();
			})
			->findOrFail($id);
		UserService::foot($id,$this->user_id);
		$this->writeJson($row);
	}
	
	
	/**
	 * 获取评价数量
	 * @param Request $request
	 * @return void
	 */
	function getGoodsAssessCount(Request $request)
	{
		$id = $request->get('id/d');
		$data = [
			'assess_count' => GoodsOrdersAssess::where(['goods_id' => $id])->select(),
			'assess_good_count' => GoodsOrdersAssess::where(['goods_id' => $id])->where(['score' => 5])->select(),
			'assess_normal_count' => GoodsOrdersAssess::where(['goods_id' => $id])->whereIn('score', [3, 4])->select(),
			'assess_bad_count' => GoodsOrdersAssess::where(['goods_id' => $id])->whereIn('score', [1, 2])->select(),
			'assess_images_count' => GoodsOrdersAssess::where(['goods_id' => $id])->where('images', '<>', '')->select(),
		];
		$this->writeJson($data);
	}
	
	/**
	 * 获取评价列表
	 * @param Request $request
	 * @return void
	 */
	function getGoodsAssess(Request $request)
	{
		$id = $request->get('id/d');
		$rows = GoodsOrdersAssess::where(['goods_id' => $id])
			->with(['user', 'orders'])
			->paginate()
			->items();
		$this->writeJson($rows);
	}
	
	/**
	 * 领取优惠券
	 * @param Request $request
	 * @return void
	 */
	function receiveCoupon(Request $request)
	{
		$id = $request->post('id/d'); //优惠券ID
		$coupon = GoodsCoupon::findOrFail($id);
		if ($coupon->num == 0) {
			$this->writeJson(0, '优惠券被领完了');
		}
		
		$row = UserCoupon::where(['user_id' => $this->user_id, 'coupon_id' => $id])->find();
		if ($row) {
			$this->writeJson(0, '不能重复领取');
		}
		
		$coupon->num -= 1;
		$coupon->save();
		UserCoupon::create([
			'user_id' => $this->user_id,
			'coupon_id' => $id,
			'coupon' => $coupon,
			'expiretime' => strtotime('+' . $coupon->expire_day . 'day', time())
		]);
		
		$this->writeJson();
	}
	
	/**
	 * 创建订单
	 * @param Request $request
	 * @return void
	 */
	function createOrder(Request $request)
	{
		$goods_id = $request->post('goods_id/d'); //商品
		$address_id = $request->post('address_id/d'); //地址
		$spec_id = $request->post('spec_id/d'); //规格
		$num = $request->post('num/d', 1); //数量
		$coupon_id = $request->post('coupon_id/d', 0); //优惠券
		$mark = $request->post('mark', ''); //备注
		$size = $request->post('size');
		$goods = Goods::findOrFail($goods_id);
		$spec = GoodsSpec::findOrFail($spec_id);
		if ($spec->sku < $num) {
			$this->writeJson(0, '商品库存不足');
		}
		
		//修改库存
		$spec->sku -= $num;
		$spec->save();
		
		$weight = bcmul((string)$goods->weight, (string)$num, 2); //总重量
		$first_freight = Config::getFieldByName('first_freight', 'value'); //首重运费
		$more_freight = Config::getFieldByName('more_freight', 'value'); //续重运费
		
		if ($weight <= 1) {
			$freight = $first_freight; //运费
		} else {
			$more_weight = ceil($weight - 1);//超出重量
			$more_freight = bcmul((string)$more_weight, $more_freight, 2);//超出的运费
			$freight = bcadd($first_freight, $more_freight, 2);//运费
		}
	
		$goods_amount = bcmul((string)$spec->price, (string)$num, 2); //商品总价格
		$total_amount = bcadd($goods_amount, $freight, 2);//订单总价格=商品总价格+运费
		
		$coupon_amount = 0; //优惠券金额
		if (!empty($coupon_id)) {
			$coupon = UserCoupon::where(['user_id' => $this->user_id])->findOrFail($coupon_id);
			if ($coupon->expiretime <= time()) {
				$this->writeJson(0, '优惠券已过期');
			}
			if ($coupon->status != 0) {
				$this->writeJson(0, '优惠券已被使用');
			}
			$couponDetail = $coupon->coupon;
			if ($couponDetail['scene'] == 1) {
				if ($couponDetail['goods_id'] != $goods_id) {
					$this->writeJson(0, '此优惠券不属于本商品');
				}
			}
			if ($couponDetail['type'] == 1 && $couponDetail['with_amount'] > $goods_amount) {
				$this->writeJson(0, '订单金额不满足满减金额');
			}
			$coupon_amount = $couponDetail['amount'];
			//使用优惠券
			$coupon->status = 1;
			$coupon->save();
		}
		
		
		$pay_amount = $total_amount - $coupon_amount;
		if ($pay_amount <= 0) {
			$pay_amount = 0.01;
		}
		$user = User::findOrFail($this->user_id);
		$order_no = Random::getOrderNumber();
		$data = [
			'user_id' => $this->user_id,
			'school_id' => $user->school_id,
			'goods_id' => $goods_id,
			'address_id' => $address_id,
			'spec_id' => $spec_id,
			'total_amount' => $total_amount, // 订单总价格
			'coupon_id' => $coupon_id,
			'freight' => $freight,//运费
			'coupon_amount' => $coupon_amount,//优惠券金额
			'goods_amount' => $goods_amount,//商品总价格
			'pay_amount' => $pay_amount, //原价-优惠券金额
			'mark' => $mark,
			'num' => $num,
			'order_no' => $order_no,
			'size' => $size
		];

		Db::startTrans();
		try {
			$res = GoodsOrders::create($data);
			//任务队列
			Queue::later($this->order_timeout, Task::class, ['orders_id' => $res->id, 'action' => 'goods_orders_timeout'], 'goods');
			Db::commit();
		} catch (\Exception $e) {
			Db::rollback();
			$this->writeJson(0, $e->getMessage());
		}
		$this->writeJson(1,'',$res->id);
	}
	
	/**
	 * 订单支付
	 * @param Request $request
	 * @return void
	 */
	function orderPay(Request $request)
	{
		$id = $request->post('id/d');
		$pay_type = $request->post('pay_type/d');
		$row = GoodsOrders::findOrFail($id);
		if ($row->status != 0) {
			$this->writeJson(0, '订单异常');
		}
		$order_no = Random::getOrderNumber();
		$row->order_no = $order_no;
		$row->pay_type = $pay_type;
		$row->save();
		$user = User::findOrFail($this->user_id);
		switch ($pay_type) {
			case 1:
			case 2:
				try {
					$ret = Pay::pay($pay_type, $row->pay_amount, $order_no, '购买商品', 'goods',$user->openid);
				} catch (\Exception $e) {
					$this->writeJson($e->getMessage());
				}
				break;
			default:
				$this->writeJson(0, '支付方式错误');
		}
		
		$this->writeJson($ret);
	}
	
	/**
	 * 订单评价
	 * @return void
	 */
	function orderAssess(Request $request)
	{
		$id = $request->post('id/d');
		$score = $request->post('score/d');
		$images = $request->post('images');
		$content = $request->post('content');
		$anonym = $request->post('anonym/d');
		$row = GoodsOrders::where(['user_id' => $this->user_id])->findOrFail($id);
		if ($row->status != 3) {
			$this->writeJson(0, '订单异常');
		}
		$row->status = 4;
		$row->save();
		//评论
		GoodsOrdersAssess::create([
			'orders_id' => $id,
			'goods_id' => $row->goods->id,
			'user_id' => $this->user_id,
			'score' => $score,
			'images' => $images,
			'content' => $content,
			'anonym' => $anonym,
		]);
		$this->writeJson();
	}
	
	
	/**
	 * 检查优惠券
	 * @param Request $request
	 * @return void
	 * @throws DataNotFoundException
	 * @throws DbException
	 * @throws ModelNotFoundException
	 */
	function checkCoupon(Request $request)
	{
		$goods_id = $request->get('goods_id/d');
		$num = $request->get('num/d');
		$spec_id = $request->get('spec_id/d');
		$spen = GoodsSpec::findOrFail($spec_id);
		$goods_amount = bcmul((string)$num, (string)$spen->price, 2);//总金额
		
		$coupon_count = UserCoupon::where(['user_id' => $this->user_id, 'status' => 0])->count();
		$coupon = UserCoupon::where(['user_id' => $this->user_id, 'status' => 0])->select();//全部优惠券
		$valid = [];//有效优惠券
		$invalid = [];//无效优惠券
		foreach ($coupon as $k) {
			$detail = $k->coupon;
			if ($k->expiretime <= time()) {
				$k->msg = '优惠券已过期';
				$invalid[] = $k;
				continue;
			}
			if ($detail['scene'] == 1) {
				//指定商品
				if ($detail['goods_id'] == $goods_id) {
					if ($detail['type'] == 1) {
						if ($detail['with_amount'] > $goods_amount) {
							//不满足
							$k->msg = '订单金额不满足满减金额';
							$invalid[] = $k;
						} else {
							//满足
							$k->msg = '666';
							$valid[] = $k;
						}
					} else {
						$k->msg = '666';
						$valid[] = $k;
					}
				} else {
					$k->msg = '不支持此商品';
					$invalid[] = $k;
				}
			} else {
				if ($detail['type'] == 1) {
					if ($detail['with_amount'] > $goods_amount) {
						//不满足
						$k->msg = '订单金额不满足满减金额';
						$invalid[] = $k;
					} else {
						//满足
						$k->msg = '666';
						$valid[] = $k;
					}
				} else {
					$k->msg = '666';
					$valid[] = $k;
				}
			}
		}
		$this->writeJson(['count' => $coupon_count, 'valid' => $valid, 'invalid' => $invalid]);
	}
	
	/**
	 * 价格计算
	 * @param Request $request
	 * @return void
	 */
	function getPrice(Request $request)
	{
		$goods_id = $request->post('goods_id/d');
		$num = $request->post('num/d');
		$coupon_id = $request->post('coupon_id/d', 0);
		$spec_id = $request->post('spec_id/d');
		$goods = Goods::findOrFail($goods_id);
		$spec = GoodsSpec::findOrFail($spec_id);
		
		if ($spec->sku < $num) {
			$this->writeJson(0, '商品库存不足');
		}
		
		$weight = bcmul((string)$goods->weight, (string)$num, 2); //总重量
		$first_freight = Config::getFieldByName('first_freight', 'value'); //首重运费
		$more_freight = Config::getFieldByName('more_freight', 'value'); //续重运费
		
		if ($weight <= 1) {
			$freight = $first_freight; //运费
		} else {
			$more_weight = ceil($weight - 1);//超出重量
			$more_freight = bcmul((string)$more_weight, $more_freight, 2);//超出的运费
			$freight = bcadd($first_freight, $more_freight, 2);//运费
		}
		
		$goods_amount = bcmul((string)$num, (string)$spec->price, 2);//商品总金额
		$total_amount = bcadd($goods_amount, $freight, 2);//订单总金额
		$coupon_amount = 0; //优惠券金额
		if (!empty($coupon_id)) {
			$coupon = UserCoupon::where(['user_id' => $this->user_id])->findOrFail($coupon_id);
			if ($coupon->expiretime <= time()) {
				$this->writeJson(0, '优惠券已过期');
			}
			if ($coupon->status != 0) {
				$this->writeJson(0, '优惠券已被使用');
			}
			$couponDetail = $coupon->coupon;
			if ($couponDetail['scene'] == 1) {
				if ($couponDetail['goods_id'] != $goods_id) {
					$this->writeJson(0, '此优惠券不属于本商品');
				}
			}
			if ($couponDetail['type'] == 1 && $couponDetail['with_amount'] > $goods_amount) {
				$this->writeJson(0, '订单金额不满足满减金额');
			}
			$coupon_amount = $couponDetail['amount'];
			
		}
		$pay_amount = $total_amount - $coupon_amount;
		if ($pay_amount <= 0) {
			$pay_amount = 0.01;
		}
		$this->writeJson(1, '', ['pay_amount' => $pay_amount, 'freight' => $freight, 'coupon_amount' => $coupon_amount, 'goods_amount' => $goods_amount, 'total_amount' => $total_amount]);
	}
	
	/**
	 * 收藏
	 * @param Request $request
	 */
	function collect(Request $request)
	{
		$id = $request->post('id');
		$res = UserService::collect($id, $this->user_id);
		$this->writeJson($res);
	}
	
	/**
	 * 加入购物车
	 * @return void
	 */
	function shopcarAdd(Request $request)
	{
		$goods_id = $request->post('goods_id/d');
		$spec_id = $request->post('spec_id/d');
		$num = $request->post('num/d', 1);
		$size = $request->post('size');
		
		$car = GoodsShopcar::where(['user_id' => $this->user_id, 'goods_id' => $goods_id, 'spec_id' => $spec_id ,'size'=>$size])->find();
		if ($car) {
			$car->num += $num;
		} else {
			GoodsShopcar::create([
				'user_id' => $this->user_id,
				'goods_id' => $goods_id,
				'spec_id' => $spec_id,
				'num' => $num,
				'size' => $size
			]);
		}
		$this->writeJson(1, '加入成功');
	}
	
	/**
	 * 购物车列表
	 * @return void
	 */
	function shopcarList()
	{
		$rows = GoodsShopcar::where(['user_id' => $this->user_id])
			->with(['goods' => function (Query $query) {
				$query->filter(function ($data) {
					$data->images = explode(',', $data->images);
					$data->videos = explode(',', $data->videos);
				});
			}, 'spec'])
			->paginate()
			->items();
		$this->writeJson($rows);
	}
	
	/**
	 * 修改购物车
	 * @param Request $request
	 * @return void
	 */
	function shopcarEdit(Request $request)
	{
		$shop_car_id = $request->post('shop_car_id/d');
		$num = $request->post('num/d', 1);
		$spec_id = $request->post('spec_id/d', 0);
		$size = $request->post('size');
		$row = GoodsShopcar::findOrFail($shop_car_id);
		if (!empty($size)) {
			$row->size = $size;
		}
		if (!empty($num)) {
			$row->num = $num;
		}
		if (!empty($spec_id)) {
			$row->spec_id = $spec_id;
		}
		$row->save();
		$this->writeJson();
	}
	
	/**
	 * 删除购物车
	 * @param Request $request
	 * @return void
	 */
	function shopcarDelete(Request $request)
	{
		$shop_car_id = $request->post('shop_car_id/d');
		$row = GoodsShopcar::findOrFail($shop_car_id);
		$row->delete();
		$this->writeJson();
	}
	
	/**
	 * 购物车创建订单
	 * @param Request $request
	 * @return void
	 * @throws Exception
	 */
	function shopcarCreateOrder(Request $request)
	{
		$shop_car_ids = $request->post('shop_car_ids');
		$pay_type = $request->post('pay_type/d');
		$address_id = $request->post('address_id/d');
		$rows = GoodsShopcar::whereIn('id', $shop_car_ids)->selectOrFail();
		$pay_amount = 0;
		$order_ids = [];
		$first_freight = Config::getFieldByName('first_freight', 'value'); //首重运费
		$more_freight = Config::getFieldByName('more_freight', 'value'); //续重运费
		$user = User::findOrFail($this->user_id);
		foreach ($rows as $row) {
			$order_no = Random::getOrderNumber();
			$goods = Goods::findOrFail($row->goods_id);
			$spec = GoodsSpec::findOrFail($row->spec_id);
			
			if ($spec->sku < $row->num) {
				$this->writeJson(0, '商品库存不足', $row->id);
			}
			//减少库存
			$spec->sku -= $row->num;
			$spec->save();
			$weight = bcmul((string)$goods->weight, (string)$row->num, 2); //总重量
			if ($weight <= 1) {
				$freight = $first_freight; //运费
			} else {
				$more_weight = ceil($weight - 1);//超出重量
				$more_freight = bcmul((string)$more_weight, $more_freight, 2);//超出的运费
				$freight = bcadd($first_freight, $more_freight, 2);//运费
			}
			$goods_amount = $row->num * $spec->price;
			$total_amount = $goods_amount + $freight;//原价 = 数量 * 规格单价 + 运费
			$res = GoodsOrders::create([
				'user_id' => $this->user_id,
				'goods_id' => $row->goods_id,
				'school_id' => $user->school_id,
				'num' => $row->num,
				'address_id' => $address_id,
				'spec_id' => $row->spec_id,
				'total_amount' => $total_amount,
				'pay_type' => $pay_type,
				'pay_amount' => $total_amount,
				'order_no' => $order_no,
				'freight' => $freight,//运费
				'goods_amount' => $goods_amount,//商品价格
				'size' => $row->size,//尺码
			]);
			$pay_amount += $total_amount;
			$order_ids[] = $res->id;
			//任务队列
			Queue::later($this->order_timeout, Task::class, ['order_id' => $res->id, 'action' => 'goods_order_timeout'], 'goods');
		}
		$order_no = Random::getOrderNumber();
		GoodsShopcarOrders::create([
			'user_id' => $this->user_id,
			'order_ids' => implode(',', $order_ids),
			'pay_type' => $pay_type,
			'order_no' => $order_no,
		]);
		$user = User::findOrFail($this->user_id);
		switch ($pay_type) {
			case 1:
			case 2:
				$ret = Pay::pay($pay_type, $pay_amount, $order_no, '购物车结算', 'shopcar',$user->openid);
				break;
			default:
				$this->writeJson(0, '支付方式错误');
		}
		//清空购物车
		$rows->delete();
		$this->writeJson($ret);
	}
	
	
	/**
	 * 购物车价格计算
	 * @param Request $request
	 * @return void
	 */
	function shopcarGetPrice(Request $request)
	{
		$shop_car_ids = $request->post('shop_car_ids');
		$rows = GoodsShopcar::whereIn('id', $shop_car_ids)->selectOrFail();
		$first_freight = Config::getFieldByName('first_freight', 'value'); //首重运费
		$more_freight = Config::getFieldByName('more_freight', 'value'); //续重运费
		$goods_amount = 0;
		$goods_freight = 0;
		foreach ($rows as $row) {
			$weight = bcmul((string)$row->goods->weight, (string)$row->num, 2);//重量
			$goods_amount = bcadd(bcmul((string)$row->num, (string)$row->spec->price, 2), (string)$goods_amount, 2); //总价格
			
			if ($weight <= 1) {
				$freight = $first_freight; //运费
			} else {
				$more_weight = ceil($weight - 1);//超出重量
				$overflow_freight = bcmul((string)$more_weight, $more_freight, 2);//超出的运费
				$freight = bcadd($first_freight, $overflow_freight, 2);//运费
			}

			$goods_freight = bcadd((string)$goods_freight, $freight, 2);
		}
		$pay_amount = bcadd($goods_amount, $goods_freight, 2);
		$this->writeJson(1, '', ['pay_amount' => $pay_amount, 'freight' => $goods_freight ,'goods_amount'=>$goods_amount]);
	}
	
	/**
	 * 获取订单列表
	 * @param Request $request
	 * @return void
	 */
	function getOrderList(Request $request)
	{
		$status = $request->get('status/d');
		$rows = GoodsOrders::with(['spec', 'goods'])
			->where(['user_id' => $this->user_id])
			->when(in_array($status, [1, 2, 3, 4]), function (Query $query) use ($status) {
				switch ($status) {
					case 1:
						$query->where('status', 0);
						break;
					case 2:
						$query->where('status', 1);
						break;
					case 3:
						$query->where('status', 2);
						break;
					case 4:
						$query->where('status', 4);
						break;
				}
			})
			->order(['id' => 'desc'])
			->paginate()
			->items();
		$this->writeJson($rows);
	}
	
	/**
	 * 获取订单详情
	 * @param Request $request
	 * @return void
	 */
	function getOrderDetail(Request $request)
	{
		$id = $request->get('id/d');
		$row = GoodsOrders::with(['goods', 'address' => function (Query $query) {
			$query->withTrashed();
		}, 'spec','deliver_express'])->filter(function ($data){
			$data->createtime_text = strtotime($data->createtime) + $this->order_timeout;
		})->findOrFail($id);
		
		$this->writeJson($row);
	}
	
	/**
	 * 取消订单
	 * @param Request $request
	 * @return void
	 */
	function orderCancel(Request $request)
	{
		$id = $request->post('id/d');
		$row = GoodsOrders::where(['user_id'=>$this->user_id])->findOrFail($id);
		if ($row->status != 0){
			$this->writeJson(0, '订单状态错误');
		}
		$row->status = 5;
		$row->save();
		$this->writeJson();
	}
	
	/**
	 * 确认收货
	 * @param Request $request
	 * @return void
	 */
	function orderConfirm(Request $request)
	{
		$id = $request->post('id/d');
		$row = GoodsOrders::where(['user_id'=>$this->user_id])->findOrFail($id);
		if ($row->status != 2){
			$this->writeJson(0, '订单状态错误');
		}
		$row->status = 3;
		$row->save();
		$this->writeJson();
	}
	
	/**
	 * 删除订单
	 * @param Request $request
	 * @return void
	 */
	function orderDelete(Request $request)
	{
		$id = $request->post('id/d');
		$row = GoodsOrders::findOrFail($id);
		if (!in_array($row->status,[4,5])){
			$this->writeJson(0, '订单状态错误');
		}
		$row->delete();
		$this->writeJson();
	}
	
	/**
	 * 获取售后电话
	 * @return void
	 */
	function getServicePhone()
	{
		$row = Config::getFieldByName('goods_service','value');
		$this->writeJson($row);
	}
	
	/**
	 * 获取快递信息
	 * @param Request $request
	 * @return void
	 */
	function getExpressInfo(Request $request)
	{
		$id = $request->get('id/d');
		$row = GoodsOrders::where(['user_id' => $this->user_id])->findOrFail($id);
		if (in_array($row->status,[0,1])){
			$this->writeJson(0, '暂无快递信息');
		}
		$ret = Express::getExpressInfo($row->deliver_express->code, $row->deliver_expresswaybill);
		$ret = json_decode($ret);
		if (isset($ret->result)){
			$this->writeJson(0, $ret->message);
		}
		$this->writeJson($ret->data);
	}
	

	
	
	
}
